package com.huawei.imedia.it.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @Auther: tsx
 * @Date: 2021/3/10 - 03 - 10 - 7:09 下午
 * @Description: com.tsx.config
 * @version: 1.0
 */
@Configuration
public class CorsConfig {
    private CorsConfiguration buildCors() {
        CorsConfiguration configuration = new CorsConfiguration();
        //请求的网址
        configuration.addAllowedOriginPattern("*");
        configuration.addAllowedHeader("*");
        configuration.addAllowedMethod("*");
        //设置跨域的请求是否是同一个session
        configuration.setAllowCredentials(true);
        return configuration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource base = new UrlBasedCorsConfigurationSource();
        base.registerCorsConfiguration("/**", buildCors());
        return new CorsFilter(base);
    }
}
